package com.start.common.details;


import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.annotation.JSONField;
import com.start.common.entity.StartBaseUser;
import com.start.common.std.IStartUserKey;
import lombok.Data;
import lombok.Getter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;

@Data
public class BaseUserDetails implements UserDetails, IStartUserKey {

    protected String userId;

    private String startUserType;

    private boolean isSuperUser = false;

    private JSONObject srcUserInfo;

    public void setSrcUserInfo(Map<String, Object> map) {
        srcUserInfo = JSONObject.parseObject(JSONObject.toJSONString(map));
    }

    @Getter
    private String userClassName;

    @JSONField(serialize = false,deserialize = false)
    public StartBaseUser getStartBaseUser() {
        if (srcUserInfo == null) {
            return null;
        }
        try {
            Class<?> aClass = Class.forName(userClassName);
            return JSONObject.parseObject(JSONObject.toJSONString(srcUserInfo), (Type) aClass);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }

    @JSONField(serialize = false,deserialize = false)
    public void setStartBaseUser(StartBaseUser startBaseUser) {
        if (startBaseUser == null) return;
        this.userClassName = startBaseUser.getClass().getName();
        String s = JSONObject.toJSONString(startBaseUser);
        srcUserInfo = JSONObject.parseObject(s);
    }


    public BaseUserDetails() {
        startUserType = getStartUserType();
    }

    public BaseUserDetails(StartBaseUser startBaseUser) {
        this.userId = startBaseUser.getId();
        this.startUserType = startBaseUser.getStartUserType();
        this.userClassName = startBaseUser.getClass().getName();
        String s = JSONObject.toJSONString(startBaseUser);
        srcUserInfo = JSONObject.parseObject(s);
    }


    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return new ArrayList<>();
    }

    @Override
    public String getPassword() {
        return srcUserInfo.getString("userPwd");
    }

    @Override
    public String getUsername() {
        return srcUserInfo.getString("userName");
    }

    @Override
    public boolean isAccountNonExpired() {
        return false;
    }

    @Override
    public boolean isAccountNonLocked() {
        Integer isLocked = srcUserInfo.getInteger("isLocked");
        return isLocked != null && isLocked != 0;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return false;
    }

    @Override
    public boolean isEnabled() {
        Integer isEnabled = srcUserInfo.getInteger("isEnabled");
        return isEnabled != null && isEnabled.equals(1);
    }


    @Override
    public String getStartUserKey() {
        return startUserType + ":" + userId;
    }
}
